import { _decorator, Color, Component } from 'cc';
const { ccclass, property } = _decorator;
import * as fgui from "fairygui-cc";
import { GameController } from '../manager/GameController';

export default class PlayerEntity {
    public node: fgui.GComponent;
    public memberId: string;
    public position: number

    public energy: number = 0

    private _display: fgui.Controller = null; //是否显示
    private _levelAniStatus: fgui.Controller = null;//能量等级及传送动画
    private _infoGameStatus: fgui.Controller = null; //显示姓名和宝石

    private _isSelfController: fgui.Controller = null;//是否是自己
    private _charactorStatus: fgui.Controller = null;//人物动画状态
    private _infoView: fgui.GComponent = null;//信息组件
    private _infoStatus: fgui.Controller = null;//信息状态

    private _nameLabel: fgui.GRichTextField = null;
    private _energyLabel: fgui.GRichTextField = null;

    public isSelf: boolean = false;

    constructor(node: fgui.GComponent, position: number) {
        this.node = node;
        this.position = position;

        this._display = this.node.getController('shuxing');
        this._levelAniStatus = this.node.getController('zhuangtai');
        this._isSelfController = this.node.getChild('n1').asCom.getController('c1');

        this._charactorStatus = this.node.getChild('n1').asCom.getController('status')
        this._infoView = this.node.getChild('n3').asCom;
        this._infoStatus = this._infoView.getController('c1')
        this._infoGameStatus = this._infoView.getController('c2');
        this._nameLabel = this._infoView.getChild('n2', fgui.GRichTextField);
        this._energyLabel = this._infoView.getChild('n3', fgui.GRichTextField);

        this._energyLabel.text = "0"

        // if (position == 0) {
        //     this.isSelf = true;
        // }

        // if (this.isSelf) {
        //     this._isSelfController.setSelectedIndex(0);
        //     this._infoStatus.setSelectedIndex(0);
        //     this._nameLabel.text = "自己"
        // }
        // else {
            this._isSelfController.setSelectedIndex(1);
        //     this._infoStatus.setSelectedIndex(1);
        // }
    }

    updateInfo(playerInfo: playerInfo) {
        this._display.selectedIndex = 1;
        this._infoGameStatus.selectedIndex = 2
        this.memberId = playerInfo.memberId;
        if (this.isSelf) {
            this._isSelfController.setSelectedIndex(0);
            this._infoStatus.setSelectedIndex(0);
            this._nameLabel.text = "自己"
        }
        else {
            this._isSelfController.setSelectedIndex(1);
            this._infoStatus.setSelectedIndex(1);
            this._nameLabel.text = playerInfo.nickName
        }
        this.updateEnergy(playerInfo.shield)
    }

    updateBoom() {
        this._levelAniStatus.setSelectedIndex(4);
        this._infoStatus.setSelectedIndex(this.isSelf ? 2 : 3)
    }

    updateEnergy(energy: number) {
        this.energy = energy
        this._energyLabel.text = `${energy}`;
        if (energy == 0) return;
        const index = Math.min(
            Math.floor(Math.max(energy, 0) / 10) + 1, // 基础区间计算
            energy > 30 ? 4 : 3 // 修正 30 的特殊情况
        );
        this._levelAniStatus.setSelectedIndex(index);
        if (energy > 30) {
            this._infoStatus.setSelectedIndex(this.isSelf ? 2 : 3)
        }
    }

    display(value: boolean) {
        this._display.setSelectedIndex(value ? 1 : 0)
    }

    playerFakeReady() {
        this.display(true);
        this._infoStatus.selectedIndex = 1;
        this._infoGameStatus.selectedIndex = 1
        this._nameLabel.text = "准备中"
    }

    resetInfo() {
        this._levelAniStatus.setSelectedIndex(0);
        this._energyLabel.text = "0"
        this._infoGameStatus.selectedIndex = 0
        this._charactorStatus.setSelectedIndex(0)
        this.isSelf = false;
        // if (this.isSelf) {
        //     this._display.selectedIndex = 1;
        //     this._isSelfController.setSelectedIndex(0);
        //     this._infoStatus.setSelectedIndex(0)
        // }
        // else {
        this._display.selectedIndex = 0;
        this._isSelfController.setSelectedIndex(1);
        this._infoStatus.setSelectedIndex(1)
        // }
    }

    playerReady() {

        this._display.selectedIndex = 1;
        this._infoGameStatus.selectedIndex = 1
        this._nameLabel.text = "准备中"

        if (this.position == 0) {
            this.isSelf = true;
            this._isSelfController.setSelectedIndex(0);
            this._infoStatus.setSelectedIndex(0);
        }
        else {
            this._isSelfController.setSelectedIndex(1);
            this._infoStatus.setSelectedIndex(1);
        }
    }

    playerTrunOn() {
        this._charactorStatus.setSelectedIndex(2);
    }

    demoTransfer() {
        this.display(false)
        this._levelAniStatus.selectedIndex = 7;
        // this._charactorStatus.setSelectedIndex(1);
        // setTimeout(() => {
        //     this._charactorStatus.setSelectedIndex(2);
        //     this._levelAniStatus.setSelectedIndex(5);
        // }, 500);

        setTimeout(() => {
            this._levelAniStatus.setSelectedIndex(0);
            // this._charactorStatus.setSelectedIndex(0);
        }, 742);
    }

    playerGameTransfer() {
        this._display.selectedIndex = 1;
        if (this.isSelf) {
            this._nameLabel.text = "自己"
        }

        this._levelAniStatus.setSelectedIndex(5);
        setTimeout(() => {
            this._infoGameStatus.selectedIndex = 2
            this._levelAniStatus.setSelectedIndex(0);
            this._charactorStatus.setSelectedIndex(1);
        }, 666);

        setTimeout(() => {
            this._charactorStatus.setSelectedIndex(2);
        }, 1166);
    }
}